<?php include("../includes/menu/index.html"); ?>
<?php include("../includes/loginHeader.php"); ?>
<link rel = "stylesheet" href = "../includes/menu/menu_style.css">
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
	<head>
		<title>Login</title>
		<style type = "text/css">
			td{
				padding-top: 2px;
				padding-bottom: 2px;
				padding-left: 10px;
				padding-right: 10px;
			}
			div{
				text-align: center
			}
			div div{
				font-size: larger;
			}
			.smalltext{
				font-size: smaller
			}
			.prompt{
				color: blue;
				font-family: sans-serif;
				font-size: smaller;
 			}
			.largeerror{
				color: red;
			}
			.error{
				color: red;
				font-size: smaller;
			}
		</style>
	</head>
	<body>
		<?php
			$formerrors = ["User_Nameerror" => false, "Passworderror" => false];
			$User_Name = "";
			$Password = "";
			$exists = "";
			
			extract($_POST);
			$iserror = false;
			$inputlist = array("User_Name" => "inputusername", "Password" => "inputpassword");
			
			if(isset($submit)){
				if($User_Name == ""){
					$formerrors["User_Nameerror"]=true;
					$iserror = true;
				}
				if($Password == ""){
					$formerrors["Passworderror"]=true;
					$iserror = true;
				}
				if(!$iserror){
					$query = "SELECT Username, Password
							FROM UsersInfo 
							WHERE Username = '" . $User_Name . "' AND Password = '" . $Password . "'; ";
					
					if( !( $database = mysql_connect("localhost", "root", "")))
						die( "Could not connect to database" );
					
					if( !mysql_select_db( "discourseanalysis", $database ))
						die( "Could not open database" );
						
					if( !( $result = mysql_query( $query, $database ))){
						print( "Could not execute query! <br />" );
						die( mysql_error() );
					}
					//$numrows = "SELECT COUNT(username) as 'exists' FROM $result; ";
					$holder = 1;
					while($display = mysql_fetch_assoc($result)){
						if($display['Username'] == $User_Name){
							session_start();
							$_SESSION['user_id'] = "$User_Name";
							print("$User_Name Logged In");
							header("Location: index.php");
							$holder = 0;
						}
					}
					if($holder == 1){
						print("Sorry Please Try Again");
						header("Location: dynamicLogin.php");
					}
					
					mysql_close( $database );
				
						print("</body></html>" );
						die();
					print("
						
					");
				}
			}
			print( "<h1>Enter Your Login And Password.</h1>" );
				
			if( $iserror ){
				print( "<br /><span class = 'largeerror'>
					Fields with * need to be filled in properly.</span>" );
			}
			//poster
			print( "<form method = 'post' action = 'dynamicLogin.php'><br />" );
			$counter = 0;
			foreach( $inputlist as $inputname => $inputalt ){				
				if($counter == 1){
					print( "&nbsp;&nbsp;&nbsp;&nbsp;$inputname : <input type = 'password'
						name = '$inputname' size = '27' value = '" . $$inputname . "' />" );
				}
				
				if($counter == 0){
					print( "$inputname : <input type = 'text'
						name = '$inputname' size = '25' value = '" . $$inputname . "' />" );
					$counter = 1;
				}
				
				if( $formerrors[ ( $inputname )."error" ] == true )
					print( "<span class = 'error'>*</span>" );
					
				print( "<br />" );
			}
			
			print( "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'submit' name = 'submit'
				value = 'Submit' /></form></body></html>" );
			
		?>